/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.xyzh05.webapp.util;

import java.awt.Color;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.patchca.color.ColorFactory;
import org.patchca.color.SingleColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.filter.predefined.DiffuseRippleFilterFactory;
import org.patchca.filter.predefined.DoubleRippleFilterFactory;
import org.patchca.filter.predefined.MarbleRippleFilterFactory;
import org.patchca.filter.predefined.WobbleRippleFilterFactory;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.utils.encoder.EncoderHelper;
import org.patchca.word.RandomWordFactory;

/**
 * 验证码生成工具
 */
public class CaptchaServlet extends HttpServlet {

    private static final long serialVersionUID = 3796351198097771007L;
    private static ConfigurableCaptchaService cs = null;
    private static ColorFactory cf = null;
    private static RandomWordFactory wf = null;
    private static Random r = new Random();
    private static CurvesRippleFilterFactory crff = null;
    private static MarbleRippleFilterFactory mrff = null;
    private static DoubleRippleFilterFactory drff = null;
    private static WobbleRippleFilterFactory wrff = null;
    private static DiffuseRippleFilterFactory dirff = null;

    @Override
    public void init() throws ServletException {
        super.init();
        cs = new ConfigurableCaptchaService();
        cf = new SingleColorFactory(new Color(25, 60, 170));
        wf = new RandomWordFactory();
        crff = new CurvesRippleFilterFactory(cs.getColorFactory());
        drff = new DoubleRippleFilterFactory();
        wrff = new WobbleRippleFilterFactory();
        dirff = new DiffuseRippleFilterFactory();
        mrff = new MarbleRippleFilterFactory();
        cs.setWordFactory(wf);
        cs.setColorFactory(cf);
        cs.setWidth(120);
        cs.setHeight(50);
    }

    @Override
    public void destroy() {
        wf = null;
        cf = null;
        cs = null;
        super.destroy();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("image/png");
        response.setHeader("cache", "no-cache");
        wf.setMaxLength(5);
        wf.setMinLength(3);
        //HttpSession session = request.getSession(true);
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            switch (r.nextInt(5)) {
                case 0:
                    cs.setFilterFactory(crff);
                    break;
                case 1:
                    cs.setFilterFactory(mrff);
                    break;
                case 2:
                    cs.setFilterFactory(drff);
                    break;
                case 3:
                    cs.setFilterFactory(wrff);
                    break;
                case 4:
                    cs.setFilterFactory(dirff);
                    break;
            }
            String captcha = EncoderHelper.getChallangeAndWriteImage(cs, "png", os);
            //session.setAttribute("captcha", captcha);
            RequestUtil.setCookie(response, "captcha", captcha, "/");
            os.flush();
        } catch (IOException ex) {
            throw ex;
        } finally {
            try {
                os.close();
            } catch (Exception x) {
            }
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "CaptchaServlet";
    }// </editor-fold>
}
